package cn.itcast.web.aspect;

import cn.itcast.domain.system.SysLog;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.SysLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class LogAspect {

    @Autowired
    private SysLogService logService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpSession session;

    /**
     * pjp ： 被代理对象的方法
     */
    @Around(value="execution( * cn.itcast.web.controller.*.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //1、增强
        SysLog log = new SysLog();
        log.setTime(new Date());
        log.setIp(request.getRemoteAddr());
        Object obj = session.getAttribute("loginUser");

        if(obj != null) {
            User user = (User) obj;
            log.setUserName(user.getUserName());
            log.setCompanyId(user.getCompanyId());
            log.setCompanyName(user.getCompanyName());
        }
        //方法标记对象
        MethodSignature ms = (MethodSignature)pjp.getSignature();
        //获取到封装的method对象
        Method method = ms.getMethod();
        String methodName = method.getName();
        log.setMethod(methodName);
        //中文注释
        if(method.isAnnotationPresent(RequestMapping.class)) {
            //方法上具有RequestMapping注解
            RequestMapping annotation = method.getAnnotation(RequestMapping.class);
            String action = annotation.name();
            log.setAction(action);
        }

        //private String action;
        logService.save(log);
        //2、调用被代理对象方法
        return pjp.proceed();
    }

    //将登录信息记录到日志
    @Around(value="execution( * cn.itcast.web.controller.*.*(..))")
    public Object loginAround(ProceedingJoinPoint pjp) throws Throwable {
        Object proceed = pjp.proceed();
        //1、增强
        SysLog log = new SysLog();
        log.setTime(new Date());
        log.setIp(request.getRemoteAddr());
        Object obj = session.getAttribute("loginUser");

        if(obj != null) {
            User user = (User) obj;
            log.setUserName(user.getUserName());
            log.setCompanyId(user.getCompanyId());
            log.setCompanyName(user.getCompanyName());
        }
        //方法标记对象
        MethodSignature ms = (MethodSignature)pjp.getSignature();
        //获取到封装的method对象
        Method method = ms.getMethod();
        String methodName = method.getName();
        log.setMethod(methodName);
        //中文注释
        if(method.isAnnotationPresent(RequestMapping.class)) {
            //方法上具有RequestMapping注解
            RequestMapping annotation = method.getAnnotation(RequestMapping.class);
            String action = annotation.name();
            log.setAction(action);
        }

        //private String action;
        logService.save(log);
        //2、调用被代理对象方法
        return proceed;
    }
}
